package com.sskj.b70.utils;

import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.sskj.b70.common.App;

import io.reactivex.android.schedulers.AndroidSchedulers;


public class ToastUtil {
    protected static Toast toast;

    private static void show(CharSequence message, boolean isShort) {
        if (toast != null) {
            toast.setText(message);
            toast.setDuration(isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
            toast.show();
        } else {
            toast = Toast.makeText(App.INSTANCE, message, isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
            toast.show();
        }
    }

    private static void show(int message, boolean isShort) {
        if (toast != null) {
            toast.setText(message);
            toast.setDuration(isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
            toast.show();
        } else {
            toast = Toast.makeText(App.INSTANCE, message, isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
            toast.show();
        }
    }

    /**
     * 长时间显示Toast
     *
     * @param message
     */
    public static void showLong(CharSequence message) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(() -> show(message, false));
    }

    public static void showShort(CharSequence message) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(() -> show(message, true));
    }

    public static void showShort(int message) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(() -> show(message, true));
    }

    public static void showLong(int message) {
        AndroidSchedulers.mainThread().createWorker().schedule(() -> {
                    show(message, true);

                }
        );
    }
}
